import { getAction, postAction, putAction } from '@/api/manage'
import Vue from 'vue'
import { UI_CACHE_DB_DICT_DATA } from '@/store/mutation-types'

const duplicateCheck = (params) => getAction('/sys/duplicate/check', params)

const sendCaptcha = (params) => getAction('/mail/tool/sendCaptcha', params)

// 字典标签专用（通过code获取字典数组）
const ajaxGetDictItems = (code, params) => getAction(`/sys/dict/getDictItems/${code}`, params)
// 获取字典数据
const ajaxGetDictText = (code, key, params) => getAction(`/sys/dict/getDictText/${code}/${key}`, params)

const ajaxGetTableDictText = (params) => getAction(`/sys/dict/loadTableDict`, params)

// 从缓存中获取字典配置
// eslint-disable-next-line no-unused-vars
function getDictItemsFromCache(dictCode) {
  if (Vue.ls.get(UI_CACHE_DB_DICT_DATA) && Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]) {
    const dictItems = Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]
    // console.log("-----------getDictItemsFromCache----------dictCode="+dictCode+"---- dictItems=",dictItems)
    return dictItems
  }
}

const queryUserRole = (params) => getAction('/sys/user/queryUserRole', params)

const queryRoleAll = (params) => getAction('/sys/role/queryall', params)

const getPermissionList = (params) => getAction('/sys/permission/list', params)
const addPermission = (params) => postAction('/sys/permission/add', params)
const editPermission = (params) => putAction('/sys/permission/edit', params)
const queryTreeList = (params) => getAction('/sys/permission/queryTreeList', params)

const queryTreeListForRole = (params) => getAction('/sys/role/queryTreeList', params)
const queryRolePermission = (params) => getAction('/sys/permission/queryRolePermission', params)
const saveRolePermission = (params) => postAction('/sys/permission/saveRolePermission', params)
const frozenBatch = (params) => putAction('/sys/user/frozenBatch', params)

// 改变密码
const changePassword = (params) => putAction('/sys/user/changePassword', params)

// 数据字典
const addDict = (params) => postAction('/sys/dict/add', params)
const editDict = (params) => putAction('/sys/dict/edit', params)
const treeList = (params) => getAction('/sys/dict/treeList', params)
const addDictItem = (params) => postAction('/sys/dictItem/add', params)
const editDictItem = (params) => putAction('/sys/dictItem/edit', params)

export {
  addDict,
  editDict,
  treeList,
  addDictItem,
  editDictItem,
  duplicateCheck,
  ajaxGetDictItems,
  getDictItemsFromCache,
  queryUserRole,
  queryRoleAll,
  getPermissionList,
  addPermission,
  editPermission,
  queryTreeList,
  queryTreeListForRole,
  queryRolePermission,
  saveRolePermission,
  frozenBatch,
  changePassword,
  sendCaptcha,
  ajaxGetDictText,
  ajaxGetTableDictText
}
